﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

public partial class register : System.Web.UI.Page
{
    
   
    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1.Items.Add("男");
        DropDownList1.Items.Add("女");
        SqlConnection sc1 = new SqlConnection(Application["sqlconnection"].ToString());
        sc1.Open();
        SqlCommand com_getmajor = sc1.CreateCommand();
        com_getmajor.CommandText = "SELECT Name from Major";
        SqlDataReader sd = com_getmajor.ExecuteReader();
        
        while (sd.Read())
        { 
            DropDownList2.Items.Add(sd["Name"].ToString());
           
        }
        sc1.Close();
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox2.Text.Length == 7)
        {
            string name = TextBox1.Text;
            string em = TextBox3.Text;
            string gd = DropDownList1.Text;
            string mj = DropDownList2.Text;
            string pas = TextBox2.Text;

            SqlConnection sc = new SqlConnection(Application["sqlconnection"].ToString());
            sc.Open();
            SqlCommand com_reg = sc.CreateCommand();
            com_reg.CommandText = "INSERT INTO Users(Name,Email,Gender,Major,Password,Status) VALUES (@name,@em,@gd,@mj,@pas,@st)";
            
            com_reg.Parameters.Add("@name", SqlDbType.Text);
            com_reg.Parameters.Add("@em", SqlDbType.Text);
            com_reg.Parameters.Add("@gd", SqlDbType.Text);
            com_reg.Parameters.Add("@mj", SqlDbType.Text);
            com_reg.Parameters.Add("@pas", SqlDbType.Text);
            com_reg.Parameters.Add("@st", SqlDbType.Text);

            com_reg.Parameters["@name"] = new SqlParameter("@name", name);
            com_reg.Parameters["@em"] = new SqlParameter("@em", em);
            com_reg.Parameters["@gd"] = new SqlParameter("@gd", gd);
            com_reg.Parameters["@mj"] = new SqlParameter("@mj", mj);
            com_reg.Parameters["@pas"] = new SqlParameter("@pas", pas);
            com_reg.Parameters["@st"] = new SqlParameter("@st", "true");

            int count=com_reg.ExecuteNonQuery();
            
            if (count == 1)
            {
                //Response.Write("<script language=javascript>alert=('注册成功！')</script>");
                Application["user_name"] = TextBox1.Text;
                Application["Email"] = TextBox3.Text;
                Application["Gender"] = DropDownList1.Text;
                Application["Major"] = DropDownList2.Text;
                Application["password"] = TextBox2.Text;
                Response.Redirect("index.aspx", true);
            }
            else
                //Response.Write("<script language=javascript>alert=('注册失败,请重新注册！')</script>");
               
            sc.Close();
        }
        else Label2.Text = "密码长度必须为7！";
    }
   
}